﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class FormMessages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Cookies.AllKeys.Contains("fguid"))
            {
                Response.Redirect("Login.aspx?prev=Messages.aspx");
            }

            if (!IsPostBack)
            {
                GetMyMessages();
            }
        }

        private void GetMyMessages()
        {
            long userId = Convert.ToInt64(Request.Cookies["fguid"].Value.Split('/')[0]);
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                var result = dataContext.GetMyMessages(userId);
                GridMessages.DataSource = result;
                GridMessages.DataBind();
            }
        }

        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imgButton = sender as ImageButton;
            long userId = Convert.ToInt64(Request.Cookies["fguid"].Value.Split('/')[0]);
            string messageGuid = imgButton.Attributes["MessageID"];
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                dataContext.DeleteMessage(messageGuid, userId);
            }

            GetMyMessages();
        }

    }
}